# Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved.
import os

from dotenv import dotenv_values
from pydantic import BaseModel, Field


class ConfigModel(BaseModel):

    UVICORN_HOST: str = Field(None, description="FastAPI 服务的IP地址")
    UVICORN_PORT: int = Field(None, description="FastAPI 服务的端口号")
    PG_HOST: str = Field(None, description="postgres的IP地址")
    PG_PORT: str = Field(None, description="postgres的端口号")
    PG_USER: str = Field(None, description="postgres的用户")
    PG_PWD: str = Field(None, description="postgres的密码")
    RAG_HOST: str = Field(None, description="rag 服务的IP地址")
    RAG_PORT: str = Field(None, description="rag 服务的端口")
    CHUNK_SIZE: int = Field(None, description="语料切割大小")

    MYSQL_USER: str = Field(None, description="mysql 服务的用户")
    MYSQL_PWD: str = Field(None, description="mysql 服务的密码")
    MYSQL_HOST: str = Field(None, description="mysql 服务的IP地址")
    MYSQL_PORT: str = Field(None, description="mysql 服务的端口")
    MYSQL_DATABASE: str = Field(None, description="mysql 服务的数据库")


class Config:
    config: ConfigModel

    def __init__(self):
        if os.getenv("CONFIG"):
            config_file = os.getenv("CONFIG")
        else:
            config_file = "./golden_seed_server/config/.env"
        self.config = ConfigModel(**(dotenv_values(config_file)))
        if os.getenv("PROD"):
            os.remove(config_file)

    def __getitem__(self, key):
        if key in self.config.__dict__:
            return self.config.__dict__[key]
        return None


config = Config()
